package cn.dankal.flyaudio.activity.search;

import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.widget.EditText;
import android.widget.FrameLayout;
import android.widget.TextView;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;

import butterknife.BindView;
import butterknife.OnClick;
import cn.dankal.basiclib.base.activity.BaseActivity;
import cn.dankal.basiclib.protocol.MainProtocol;
import cn.dankal.basiclib.util.FileUtils;
import cn.dankal.basiclib.widget.titlebar.SingleTextTitle;
import cn.dankal.flyaudio.R;
import cn.dankal.flyaudio.activity.search.searchfragment.SearchFragment;


/**
 * @author ricky
 * @date 2018/8/14 0014
 */
@Route(path = MainProtocol.SEARCH)
public class SearchActivity extends BaseActivity implements View.OnClickListener {


    @BindView(R.id.fl_fragment)
    FrameLayout flFragment;
    @BindView(R.id.et_input_activity_name)
    EditText etInputGoodsName;
    @BindView(R.id.tv_search)
    TextView tvSearch;
    private SearchFragment searchFragment;
    private SearchResultFragment searchResultFragment;


    @Override
    protected int getLayoutId() {
        return R.layout.activity_search;
    }

    @Override
    protected void initComponents() {

        addTitleBar(new SingleTextTitle("搜索"));
        ARouter.getInstance().inject(this);
        searchFragment = new SearchFragment(this);
        searchResultFragment = new SearchResultFragment();
        //添加fragment
        getSupportFragmentManager().beginTransaction()
                .add(R.id.fl_fragment, searchResultFragment).add(R.id.fl_fragment, searchFragment).hide(searchResultFragment).commit();
        etInputGoodsName.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
            }

            @Override
            public void afterTextChanged(Editable s) {
                //当搜索输入内容发生改变,更新
                if (searchFragment.isHidden()) {
                    getSupportFragmentManager().beginTransaction().hide(searchResultFragment).show(searchFragment).commit();
                    updateSearchHistory();
                }

            }
        });

    }

    /**
     * 搜索点击事件
     */
    @OnClick(R.id.tv_search)
    public void onViewClicked() {
        String goodsName = etInputGoodsName.getText().toString().trim();
        if (TextUtils.isEmpty(goodsName)) {
            showToast("搜索内容不能为空");
            return;
        }
        if (!TextUtils.isEmpty(goodsName) && !FileUtils.isContain(this, "search_history", goodsName))
            FileUtils.saveContent(this, "search_history", goodsName);
        //更新搜索历史
        updateSearchHistory();
        searchResultFragment.setSearchName(goodsName);
        getSupportFragmentManager().beginTransaction().hide(searchFragment).show(searchResultFragment).commit();

    }

    /**
     * 更新历史搜索记录
     */
    public void updateSearchHistory() {

        searchFragment.updateSearchHisgtoryList();

    }


    /**
     * 点击热门搜索词或者历史搜索词,将获取到的词显示在搜索输入框
     *
     * @param v
     */
    @Override
    public void onClick(View v) {
        TextView textView = (TextView) v;
        etInputGoodsName.setText(textView.getText().toString().trim());
        //光标置后
        int lenght = textView.getText().toString().trim().length();
        if (lenght > 25) {
            lenght = 25;
        }
        etInputGoodsName.setSelection(lenght);
    }
}




